//
//  MySportCircleView.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/7/10.
//

import SwiftUI
import URLImage
import ToastUI

struct MySportCircleView: View {
    @Binding var dynamicitem: DynamicItem
    @StateObject var proModel = profileViewModel()
    @State private var isExpanded = false
    @State private var showToast = false
    @State private var isLike: Bool
    init(dynamicitem: Binding<DynamicItem>) {
        _dynamicitem = dynamicitem
        _isLike = State(initialValue: dynamicitem.wrappedValue.liked)
    }
    
    var body: some View {
        VStack(alignment: .leading ,spacing: 16) {
            // 正文内容
            Text(dynamicitem.content)
                .font(.system(size: 16))
                .foregroundStyle(.white)
                .padding(.horizontal,20)
                .padding(.top,12)
                .lineLimit(isExpanded ? nil : 3)
                .onTapGesture { isExpanded.toggle() }
            
            if let address = dynamicitem.address,!address.isEmpty {
                ZStack {
                    Image("map")
                        .resizable()
                        .frame(height: 74)
                    
                }.padding(.horizontal,20)
            }

            // 九宫格图片
            if !dynamicitem.mediaUrls.isEmpty {
                PostImagesView(images: dynamicitem.mediaUrls)
                    .padding(.horizontal,20)
            }
            // 点赞评论栏
            HStack(spacing: 40) {
                Button(action: {
                    isLike.toggle()
                    if isLike {
                        proModel.dynamicLike(dynamicId: dynamicitem.dynamicId)
                    }else {
                        proModel.dynamicUnlike(dynamicId: dynamicitem.dynamicId)
                    }
                }) {
                    Label("\(dynamicitem.likeCount)", image: isLike ? "like" : "dianzan")
                }
                Button(action: {
                    
                    
                }) {
                    Label("\(dynamicitem.commentCount)", image: "pinglun")
                }
                Spacer()
                
                if (dynamicitem.userId == Int( UserConfigManager.shared.userId)) {
                    Button(action: {
                        //删除
                        
                    }) {
                        Image("shanchu")
                    }
                }
            }
            .foregroundColor(.white)
            .padding(.horizontal,20)
            .padding(.top,16)
            .padding(.bottom,20)
            .font(.system(size: 16))
            .toast(isPresented: $showToast) {
                ShowMessageView(message: proModel.upError)
            }
            .onChange(of: proModel.upError) {
                newValue in
                if let errorMessage = newValue, !errorMessage.isEmpty {
                    showToast = true
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                        showToast = false
                        proModel.upError = nil
                    }
                }
            }
            .onChange(of: proModel.isLikeSuccess) {isSuccess in
                if isSuccess {
                        // 点赞成功的处理逻辑
                        print("操作成功")
                    dynamicitem.likeCount  += isLike ? 1 : -1
                    dynamicitem.liked = isLike
                    proModel.isLikeSuccess = false
                    }
            }
            .onChange(of: proModel.isUnlikeSuccess) {isSuccess in
                if isSuccess {
                        // 点赞成功的处理逻辑
                        print("操作成功")
                    dynamicitem.likeCount  += isLike ? 1 : -1
                    dynamicitem.liked = isLike
                    proModel.isUnlikeSuccess = false
                    }
            }
        }
        .background(Color(hex: "#1E1E1E"))
        .cornerRadius(12)
        .padding(.horizontal,12)
        .onAppear {
            print(dynamicitem.avatar!)
        }
        
    }
}

//#Preview {
//    MySportCircleView()
//}
